package com.example.notificationsdemo;

import android.support.v7.app.ActionBarActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends ActionBarActivity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.button1).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				notificationDefault();
			}
		});

		findViewById(R.id.button2).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				notificationCustom();
			}
		});
	}

	protected void notificationCustom()
	{
		// prepare intent which is triggered if the
		// notification is selected

		Intent intent = new Intent(this, NotificationReceiverActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

		// build notification
		// the addAction re-use the same intent to keep the example short
		Notification n = new Notification.Builder(this).setContentTitle("New mail from " + "test@gmail.com").setContentText("Subject").setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
				.build();

		// Using RemoteViews to bind custom layouts into Notification
		RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.big_notification);
		remoteViews.setOnClickPendingIntent(R.id.button1, pIntent);
		n.bigContentView = remoteViews;
		remoteViews = new RemoteViews(getPackageName(), R.layout.small_notification);
		n.contentView = remoteViews;

		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		// hide the notification after its selected
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.FLAG_NO_CLEAR;
		notificationManager.notify(0, n);

	}

	private void notificationDefault()
	{
		// prepare intent which is triggered if the
		// notification is selected

		Intent intent = new Intent(this, NotificationReceiverActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

		// build notification
		// the addAction re-use the same intent to keep the example short
		Notification n = new Notification.Builder(this).setContentTitle("New mail from " + "test@gmail.com").setContentText("Subject").setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
				.addAction(R.drawable.ic_launcher, "Call", pIntent).addAction(R.drawable.ic_launcher, "More", pIntent).addAction(R.drawable.ic_launcher, "And more", pIntent).build();

		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		// hide the notification after its selected
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		n.flags |= Notification.FLAG_NO_CLEAR;
		notificationManager.notify(0, n);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
